import pandas as pd
from datetime import datetime
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt


# 无量纲化
def dimensionlessProcessing(df):
    newDataFrame = pd.DataFrame(index=df.index)
    columns = df.columns.tolist()
    for c in columns:
        d = df[c]
        MAX = d.max()
        MIN = d.min()
        MEAN = d.mean()
        newDataFrame[c] = ((d - MEAN) / (MAX - MIN)).tolist()
    return newDataFrame

def GRA_ONE(gray, m=0):
    # 读取为df格式
    gray = dimensionlessProcessing(gray)
    # 标准化
    std = gray.iloc[:, m]  # 为标准要素
    gray.drop(str(m), axis=1, inplace=True)
    ce = gray.iloc[:, 0:]  # 为比较要素
    shape_n, shape_m = ce.shape[0], ce.shape[1]  # 计算行列

    # 与标准要素比较，相减
    a = np.zeros([shape_m, shape_n])
    for i in range(shape_m):
        for j in range(shape_n):
            a[i, j] = abs(ce.iloc[j, i] - std[j])

    # 取出矩阵中最大值与最小值
    c, d = np.amax(a), np.amin(a)

    # 计算值
    result = np.zeros([shape_m, shape_n])
    for i in range(shape_m):
        for j in range(shape_n):
            result[i, j] = (d + 0.5 * c) / (a[i, j] + 0.5 * c)

    # 求均值，得到灰色关联值,并返回
    result_list = [np.mean(result[i, :]) for i in range(shape_m)]
    result_list.insert(m, 1)
    return pd.DataFrame(result_list)


def GRA(DataFrame):
    df = DataFrame.copy()
    list_columns = [
        str(s) for s in range(len(df.columns)) if s not in [None]
    ]
    df_local = pd.DataFrame(columns=list_columns)
    df.columns = list_columns
    for i in range(len(df.columns)):
        df_local.iloc[:, i] = GRA_ONE(df, m=i)[0]
    df_local.rename(columns={'0': 'hour', '1': 'weekday', '2': 'dest_lng', '3': 'dest_lat', '4': 'starting_lng',
                             '5': 'starting_lat', '6': 'normal_time', '7': 'driver_product_id'},
                    inplace=True)
    df_local.index = ['hour', 'weekday', 'dest_lng', 'dest_lat', 'starting_lng', 'starting_lat', 'normal_time',
                      'driver_product_id']
    return df_local


def ShowGRAHeatMap(DataFrame):
    colormap = plt.cm.RdBu
    ylabels = DataFrame.columns.values.tolist()
    f, ax = plt.subplots(figsize=(14, 14))
    ax.set_title('GRA HeatMap')
    # 设置展示一半，如果不需要注释掉mask即可
    mask = np.zeros_like(DataFrame)
    mask[np.triu_indices_from(mask)] = True
    with sns.axes_style("white"):
        sns.heatmap(DataFrame,
                    cmap="YlGnBu",
                    annot=True,
                    mask=mask,
                    )
    plt.show()

def ShowGRAHeatMapAll(DataFrame):
    colormap = plt.cm.RdBu
    ylabels = DataFrame.columns.values.tolist()
    f, ax = plt.subplots(figsize=(14, 14))
    ax.set_title('GRA HeatMap')
    with sns.axes_style("white"):
        sns.heatmap(DataFrame,
                    cmap="YlGnBu",
                    annot=True,
                    )
    plt.show()


if __name__ == '__main__':
    df = pd.read_excel('./data/driver_product_id.xlsx')
    data = df.values
    df = df[
        ['dwv_order_make_haikou_1.dest_lng', 'dwv_order_make_haikou_1.dest_lat', 'dwv_order_make_haikou_1.starting_lng',
         'dwv_order_make_haikou_1.starting_lat', 'dwv_order_make_haikou_1.normal_time',
         'dwv_order_make_haikou_1.departure_time', 'dwv_order_make_haikou_1.driver_product_id']]
    df.insert(loc=0, column='weekday', value=df.apply(
        lambda x: datetime.strptime(x['dwv_order_make_haikou_1.departure_time'], "%Y-%m-%d-%H:%M:%S").weekday(),axis='columns'))
    df.insert(loc=0, column='hour', value=df.apply(
        lambda x: datetime.strptime(x['dwv_order_make_haikou_1.departure_time'], "%Y-%m-%d-%H:%M:%S").hour,axis='columns'))
    df = df.drop(['dwv_order_make_haikou_1.departure_time'], axis=1)
    result = GRA(df)
    ShowGRAHeatMapAll(result)
